package com.example.lancooznbk.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.TemplateEntity;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangminglong on 17/8/30.
 */

public class TemplateAdapter extends BaseRecyclerAdapter<TemplateAdapter.ViewHolder>{
    private Context mContext;
    private List<TemplateEntity> mList;


    public TemplateAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
    }


    public interface OnTemplateCheckListener{
        void onCheck(TemplateEntity templateEntity);
    }
    private OnTemplateCheckListener onTemplateCheckListener;
    public void setTemplateCheck(OnTemplateCheckListener onTemplateCheckListener){
        this.onTemplateCheckListener = onTemplateCheckListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {

        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_template_setting, parent, false);

        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, boolean isItem) {
        holder.itemOuter.setTag(position);
        holder.itemOuter.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                onTemplateCheckListener.onCheck(mList.get((int) rippleView.getTag()));
                for (int i = 0 ; i < mList.size() ; i++){
                    mList.get(i).setIsChecked(i == ((int) rippleView.getTag()));
                }
                notifyDataSetChanged();
            }
        });
        TemplateEntity templateEntity = mList.get(position);
        int backgroundResId = 0;
        switch (position){
            case 0:
                backgroundResId =  R.mipmap.bg_template_item1;
                break;
            case 1:
                backgroundResId = R.mipmap.bg_template_item2;
                break;
            case 2:
                backgroundResId = R.mipmap.bg_template_item3;
                break;
        }

        holder.itemInner.setBackgroundResource(backgroundResId);
        holder.imgIcon.setImageResource(templateEntity.getIsChecked() ? R.mipmap.ic_template_item_checked : R.mipmap.ic_template_item_unchecked);
        int imgVisible = templateEntity.getIsChecked() ?  View.VISIBLE : View.INVISIBLE;
        holder.imgChecked.setVisibility(imgVisible);
        holder.imgTagChecked.setVisibility(imgVisible);
        StringBuilder builderA = new StringBuilder();
        StringBuilder builderB = new StringBuilder();
        for (int i = 0 ; i < templateEntity.getTempletDetail().size(); i++){
            for (int j = 0 ;j < templateEntity.getTempletDetail().get(i).getContent().size();j++){
                TemplateEntity.TempletDetailBean.ContentBean contentBean = templateEntity.getTempletDetail().get(i).getContent().get(j);
                int templateStrResId =  R.string.template_number_unchecked;
               /* Drawable templateDrawable;
                int textColorRes;
                if (templateEntity.getIsChecked()){
                   // templateStrResId =  R.string.template_number_checked;
                    templateDrawable = ContextCompat.getDrawable(mContext,R.drawable.bg_template_checked);
                    textColorRes = ContextCompat.getColor(mContext,R.color.green);
                }else{
                  //  templateStrResId =  R.string.template_number_unchecked;
                    templateDrawable = ContextCompat.getDrawable(mContext,R.drawable.bg_template_unchecked);
                    textColorRes = ContextCompat.getColor(mContext,R.color.blue);
                }*/

                //temlateDrawable.setBounds(0,0,temlateDrawable.getIntrinsicWidth(),temlateDrawable.getIntrinsicHeight());
              /*  holder.tvTemplateATag.setTextColor(textColorRes);
                holder.tvTemplateBTag.setTextColor(textColorRes);
                holder.tvTemplateATag.setBackground(templateDrawable);
                holder.tvTemplateBTag.setBackground(templateDrawable);*/

                String templateStr = String.format(mContext.getString(templateStrResId),contentBean.getTypeName());
               if (i == 0) {
                   builderA.append(templateStr);
                   if (j != templateEntity.getTempletDetail().size() - 1) {
                       builderA.append("\t\t");
                   }
               }else if(i == 1){
                   builderB.append(templateStr);
                   if (j != templateEntity.getTempletDetail().size() - 1) {
                       builderB.append("\t\t");
                   }
               }
            }
        }

        holder.tvTemplateA.setText(Html.fromHtml(builderA.toString()));
        holder.tvTemplateB.setText(Html.fromHtml(builderB.toString()));
        holder.tvTitle.setText(templateEntity.getTempletName());
    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    public TemplateEntity getCheckItem() {
        TemplateEntity templateEntity = null;
        for (int i = 0 ; i < mList.size() ; i++){
          if (mList.get(i).getIsChecked()){
              templateEntity = mList.get(i);
              break;
          }
        }
        return templateEntity;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout itemInner;
        private RippleView itemOuter;
        private ImageView imgIcon;
        private TextView tvTitle;
        private ImageView imgTagChecked;
        private ImageView imgChecked;
        private TextView tvTemplateA;
        private TextView tvTemplateB;
        //private TextView tvTemplateATag;
        //private TextView tvTemplateBTag;
        public ViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem){
                itemOuter = (RippleView) itemView.findViewById(R.id.item_ripper);;
                itemInner = (RelativeLayout) itemView.findViewById(R.id.item_rel);
                imgIcon = (ImageView) itemView.findViewById(R.id.img_item_template);
                tvTitle = (TextView) itemView.findViewById(R.id.tv_template_title);
                imgTagChecked = (ImageView) itemView.findViewById(R.id.img_template_selected);
                imgChecked = (ImageView) itemView.findViewById(R.id.img_template);
                tvTemplateA = (TextView) itemView.findViewById(R.id.tv_template_A);
                tvTemplateB = (TextView) itemView.findViewById(R.id.tv_template_B);
                //tvTemplateATag = (TextView) itemView.findViewById(R.id.tv_template_A_tag);
                //tvTemplateBTag = (TextView) itemView.findViewById(R.id.tv_template_B_tag);
            }
        }
    }

    public void add(List<TemplateEntity> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }
}
